<template>
  <div class="source-card-wrapper" @click="gotoSource">
    <div class="source-intro">
      <div class="pub-info">
        <em class="pointableBlue" @click.stop="gotoAuthor">
          {{ source.authorName }}
        </em>
        <span style="color: var(--gray); padding: 0 5px">|</span>
        <em>
          {{ formatDate }}
        </em>
        <span style="color: var(--gray); padding: 0 5px">|</span>
        <ul class="tags" :title="source.tag.join(',')">
          <li v-for="(tag, idx) of source.tag" :key="idx">
            {{ tag }}
            <span> · </span>
          </li>
        </ul>
      </div>
      <h3 class="source-title">
        {{ source.title }}
      </h3>
      <h3 class="source-abstract">
        {{ source.digest }}
      </h3>
      <ul class="datas">
        <li v-for="(data, idx) in datas" :key="idx">
          <i class="iconfont" :class="data.iconClass"></i>
          <span>{{ data.data }}</span>
        </li>
      </ul>
    </div>

    <div class="source-main" @click="gotoSource">
      <img :src="source.cover || fileIcon" alt="pdf" />
      <a class="hover"></a>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { reactive, computed } from 'vue'
import { format } from '@/utils/time'
import { post } from '@/utils/request'
import { confirm } from '@/utils/popup'
import config from '@/config'
import { getFileIcon } from '@/utils/img'

const props = defineProps<{
  source: App.Source
}>()

let datas = reactive([
  {
    iconClass: 'icon-eye',
    data: props.source.downloadTimes,
  },
  {
    iconClass: 'icon-like',
    data: props.source.likeTimes,
  },
  {
    iconClass: 'icon-comment',
    data: props.source.commentTimes,
  },
  {
    iconClass: 'icon-collect',
    data: props.source.collectTimes,
  },
])

let formatDate = computed(() => {
  return format(props.source.publishDate, 'yyyy-m-d')
})

function gotoSource() {
  open(`/source/${props.source.id}`)
}

function gotoAuthor() {
  open(`/author/${props.source.authorId}/profile/source`)
}

function deleteSource() {
  confirm('确认删除这篇资源吗?').then((flag) => {
    if (flag) {
      post('/source/content/delete', {
        id: props.source.id,
      })
    }
  })
}

let fileIcon = computed(() => {
  return getFileIcon(props.source.name)
})
</script>

<style lang="scss" scoped>
$h: $sourceCardHeight;
$w: $sourceCardWidth;

.source-card-wrapper {
  --scw: var(--sourceCardWidth);
  --sch: var(--sourceCardHeight);
  box-sizing: border-box;
  width: var(--scw);
  height: var(--sch);
  padding: calc(var(--sch) * 0.1) calc(var(--scw) * 0.04);

  &:hover {
    background-color: rgb(249, 249, 249);
    cursor: pointer;
  }

  .source-intro {
    float: left;
    width: 60%;
    text-align: left;
    pointer-events: none;

    .pub-info {
      font-size: calc(var(--sch) * 0.09);
      height: calc(var(--sch) * 0.17);
      line-height: calc(var(--sch) * 0.17);
      overflow: hidden;
      display: flex;
      justify-content: left;

      * {
        flex-shrink: 0;
      }

      .tags {
        li {
          float: left;
          font-size: calc($h * 0.04);
          padding: 0 calc($h * 0.03);
          color: var(--dark);
        }
      }
    }

    .source-title {
      font-size: calc(var(--sch) * 0.11);
      font-weight: bold;
      color: var(--dark);
      height: calc(var(--sch) * 0.22);
      line-height: calc(var(--sch) * 0.22);
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
      margin-bottom: calc(var(--sch) * 0.05);
    }

    .source-abstract {
      font-size: calc(var(--sch) * 0.09);
      color: var(--dark);
      line-height: 1.5;
      display: -webkit-box;
      overflow: hidden;
      -webkit-line-clamp: 2;
      -webkit-box-orient: vertical;
    }

    .datas {
      margin-top: calc($h * 0.04);

      display: flex;
      justify-content: left;
      align-items: center;

      li {
        i,
        span {
          font-size: calc($h * 0.02);
        }

        span {
          margin-left: calc($h * 0.04);
        }

        &:not(:first-child) {
          margin-left: calc($w * 0.05);
        }
      }
    }
  }

  .source-main {
    float: right;
    margin-right: 3%;
    height: 100%;
    padding: 0 5px;
    display: flex;
    justify-content: center;
    align-items: center;

    &:hover {
      cursor: pointer;

      .hover {
        display: block;
      }
    }

    img {
      height: 90%;
    }

    .hover {
      display: none;
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      border-radius: 5px;
      background-color: var(--light);
    }
  }
}
</style>
